class Node
  attr_accessor(:id,:neighbors,:flag)
  
  def initialize(id)
    @id=id
    @neighbors = [];
    @flag = :opened;
  end

  def dfs
    if(@flag != :opened)
      return;
    end
    print "#{@id} "
    @flag=:closed;
    go_through = @neighbors.select {|v| v.flag==:opened}
    if go_through == nil
      return
    end
    go_through.each {|v| v.dfs}

  end

  def get_neighbor(id)
    tmp = @neighbors.select { |x| Integer(x.id)==Integer(id) }
    if(tmp==nil)
      return nil;
    else
      return tmp[0];
    end

  end

  def add_neighbor(node)
    if get_neighbor(node.id) == nil
      @neighbors = @neighbors << node
    end
  end

  def to_s
    out = "Node id: #{@id} flag: #{@flag} neighbors: "
    @neighbors.each { |n| out += n.id+" " }
    out += "\n"
    out
  end
end